<template>
  <a-modal
    v-model="selfvisible"
    width="900px"
    :title="modalTitle"
    @cancel="handleCancel"
    @ok="handleOk"
    :okText="modalType ? '确定' : '确定选择'"
  >
    <searchData :key="modalType" :search-data="searchArr" @search="searchUnit" />
    <b-table
      ref="modalTtable"
      bordered
      :columns="modalColumns"
      :request="tableData"
      :selectConfig="{ use: true, type: 'radio', rowKey: 'id' }"
      @row-click="hanldeRadioClick"
    >
    </b-table>
    <div></div>
  </a-modal>
</template>

<script>
import { getLabelTypes } from '@/api/commons'
import { getDesignResourcesList } from '@/api/design/parameter'
import { getDrawDesignList } from '@/api/technical/designFM'

import { searchUnitArr, modalUnitColumns, searchMaterialArr, modalMaterialColumns } from './model.js'
import BTable from '../../../../components/BComponents/BTable'
export default {
  components: { BTable },
  props: {
    visible: {
      type: Boolean,
      required: false
    },
    form: Object,
    /**
     * @description: 判断是深化设计单位名称modal/深化设计资料modal
     *  @value:  true:深化设计单位名称
     *  @value:  false：深化设计资料
     */
    modalType: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      selfvisible: false,
      searchUnitArr, //深化设计单位搜索
      modalUnitColumns, //深化设计单位table
      searchMaterialArr, //深化设计资料搜索
      modalMaterialColumns, //深化设计资料table
      selectedRowKey: [],
      searchArr: this.modalType ? searchUnitArr : searchMaterialArr,
      modalColumns: this.modalType ? modalUnitColumns : modalMaterialColumns,
      modalTitle: this.modalType ? '选择设计资源' : '选择深化设计图纸', //标题
      selectedRadio: {}, //table选中数据
      searchDatas: {}, // 查询参数
      tableData: (parameter) => {
        if (this.modalType) {
          const searchData = Object.assign({}, parameter, this.searchData)
          // 返回设计单位名称（选择设计资源）table
          return getDesignResourcesList(searchData)
        } else {
          const query = {
            drawTypeId: '759320991137665024',
            projectId: this.form.projectId,
            status: 'PAST',
            deepDrawTypeId: this.form.deepDrawTypeId,
            ...this.searchData
          }
          const searchData = Object.assign({}, parameter, { query })
          // searchData.status = 'PAST'
          // 返回设计资料（深化设计图纸）table
          return getDrawDesignList(searchData)
        }
      }
    }
  },
  created() {
    this.getTypes()
  },
  watch: {
    visible: {
      handler(val) {
        this.selfvisible = val
      },
      immediate: true
    },
    modalType: {
      handler(e) {
        this.selectedRadio = {}
        if (e) {
          this.modalTitle = '选择设计资源'
          this.searchArr = searchUnitArr
          this.modalColumns = modalUnitColumns
        } else {
          this.modalTitle = '选择深化设计图纸'
          this.searchArr = searchMaterialArr
          this.modalColumns = modalMaterialColumns
        }
      },
      immediate: true
    }
  },
  methods: {
    /**
     * 表单取消
     **/
    handleCancel(e) {
      this.$emit('handelShow', false)
      this.selectedRadio = {}
    },
    /**
     * 表单确认
     **/
    handleOk(e) {
      if (!this.selectedRadio.id && !this.modalType) {
        return this.$message.warning({ content: '请选择深化设计图纸' })
      }
      this.$emit('handleClick', this.selectedRadio, this.modalType)
      this.$emit('handelShow', false)
      this.selectedRadio = {}
    },
    /**
     * @description: 搜索
     **/
    searchUnit(value) {
      this.searchData = value || {}
      this.$refs.modalTtable.getData(null, true)
    },
    /**
     * @description: 获取设计资源类型 和 设计资源等级 的选项
     **/
    getTypes() {
      getLabelTypes({ type: 13 }).then((res) => {
        this.searchArr[2].data = this.getChangeDatatTypes(res.data)
      })

      getLabelTypes({ type: 14 }).then((res) => {
        this.searchArr[1].data = this.getChangeDatatTypes(res.data)
      })
    },
    /**
     * @description: 单选项目
     **/
    hanldeRadioClick(e, d) {
      this.selectedRadio = d
    }
  }
}
</script>

<style scoped>
</style>